﻿package view.task
{
    import __AS3__.vec.*;
    import cmd.*;
    import com.core.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instance.*;
    import view.*;
    import view.message.*;

    public class ActiveTrackItems extends Box
    {
        private var cfgActive:Object;
        private var allActives:Array;
        private var lastserverTime:int = 0;
        private var endtime:int;
        private var notice:Dictionary;
        private var _showmc:int = 0;
        private var eff:Clip;
        private var _effLoading:Boolean = false;
        private var _items:Vector.<ActiveItemUI>;
        private var oldid:int = 0;
        private var _int:int = 0;

        public function ActiveTrackItems()
        {
            var _loc_1:String = null;
            var _loc_2:ActiveItemUI = null;
            this.allActives = [];
            this.notice = new Dictionary();
            this.cfgActive = ConfigDictionary.data.gameactive;
            for (_loc_1 in this.cfgActive)
            {
                
                if (this.cfgActive[_loc_1].type == 1)
                {
                    this.allActives.push(this.cfgActive[_loc_1]);
                    this.cfgActive[_loc_1].sort = int(this.cfgActive[_loc_1].sort);
                }
            }
            this.allActives.sortOn(["sort"], Array.NUMERIC);
            this._items = new Vector.<ActiveItemUI>;
            _loc_2 = new ActiveItemUI();
            this.addChild(_loc_2);
            this._items.push(_loc_2);
            _loc_2.btn.clickHandler = new Handler(this.transHandler, [0]);
            this.addEventListener(MouseEvent.ROLL_OVER, this.touchHandler2);
            this.addEventListener(MouseEvent.ROLL_OUT, this.touchHandler2);
            _loc_2.btn.x = 182;
            _loc_2.y = 5;
            _loc_2.x = 5;
            _loc_2 = new ActiveItemUI();
            this._items.push(_loc_2);
            _loc_2.btn.clickHandler = new Handler(this.transHandler, [1]);
            _loc_2.y = 25;
            _loc_2.x = 5;
            SecondTimerUtil.instance.addSecondTask(this.updateActive);
            return;
        }// end function

        private function touchHandler2(event:MouseEvent) : void
        {
            var _loc_2:Point = null;
            var _loc_3:Object = null;
            if (event.type == MouseEvent.ROLL_OVER)
            {
                _loc_2 = new Point(mouseX, mouseY);
                _loc_3 = this._items[0];
                if (_loc_3 && _loc_3.data)
                {
                    ModelProxy.change("tips", "huodong", {data:_loc_3.data, p:localToGlobal(_loc_2)});
                }
            }
            else
            {
                ModelProxy.change("tips", "state", "close");
            }
            return;
        }// end function

        private function onComp(param1) : void
        {
            this._effLoading = true;
            this.eff = new Clip("png.speff.horn", 12, 1);
            this.eff.autoPlay = true;
            this.eff.sizeGrid = "20,20,20,20";
            this.eff.width = 270;
            this.eff.height = 38;
            this.eff.blendMode = BlendMode.ADD;
            this.eff.interval = 160;
            this.eff.mouseChildren = false;
            this.eff.mouseEnabled = false;
            this.showmc = this._showmc;
            return;
        }// end function

        override public function get height() : Number
        {
            if (this._showmc == 0)
            {
                return 0;
            }
            return 25;
        }// end function

        private function transHandler(param1:int) : void
        {
            var data:Object;
            var i:* = param1;
            data = this._items[i].data;
            var level:* = UserData.getUserData("level");
            if (data.level > level)
            {
                WarningManager.show(151, [data.level]);
                return;
            }
            if (!this._items[0].hasStart)
            {
                ModelProxy.change("movetips", "error", "活动尚未开启，禁止传送");
                return;
            }
            if (data && data.npcid)
            {
                Alert2.show("即将传送，是否确认前往！", "温馨提示", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                var _loc_3:Array = null;
                if (param1 == 0)
                {
                    if (data.mapid > 0)
                    {
                        PluginManager.instance.sendToServer("instance", CMDInstance.HuoDongApp, data.mapid);
                    }
                    else
                    {
                        _loc_3 = [];
                        _loc_3 = data.npcid.split("#");
                        if (_loc_3[0] > 0)
                        {
                            ModelProxy.change("data", "taskfindpath", _loc_3[0]);
                        }
                        ModelProxy.change("zone", "stopGuaji", null);
                        PluginManager.instance.sendToServer("activity", CMDActivity.Trans, {x:_loc_3[1], y:_loc_3[2], tostageid:_loc_3[3], free:1});
                        ModelProxy.change("zone", "autoMove", {x:_loc_3[1], y:_loc_3[2], tostageid:_loc_3[3], onhorse:true});
                    }
                }
                return;
            }// end function
            );
            }
            return;
        }// end function

        public function update() : void
        {
            this.updateActive(true);
            return;
        }// end function

        private function updateActive(param1:Boolean = false) : void
        {
            var _loc_5:Object = null;
            var _loc_22:Object = null;
            var _loc_23:int = 0;
            var _loc_24:String = null;
            var _loc_25:Boolean = false;
            var _loc_26:Boolean = false;
            var _loc_27:int = 0;
            var _loc_28:Array = null;
            var _loc_29:Array = null;
            var _loc_30:Array = null;
            var _loc_31:String = null;
            var _loc_32:Array = null;
            var _loc_33:Array = null;
            var _loc_34:Array = null;
            var _loc_35:Date = null;
            var _loc_36:Date = null;
            var _loc_37:Date = null;
            var _loc_2:* = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            var _loc_3:* = new Date();
            _loc_3.setTime(_loc_2 * 1000);
            var _loc_4:int = 0;
            while (_loc_4 < this._items.length)
            {
                
                if (this._items[_loc_4].data)
                {
                    if (this._items[_loc_4].hasStart)
                    {
                        if (this._items[_loc_4].endtime - _loc_2 < 0)
                        {
                            param1 = true;
                        }
                        if (!this.notice[this._items[_loc_4].data.id])
                        {
                            ModelProxy.change("data", "activeid", this._items[_loc_4].data.id);
                            this.delayOpen(this._items[_loc_4].data);
                            this.notice[this._items[_loc_4].data.id] = true;
                        }
                    }
                    else if (this._items[_loc_4].starttime < _loc_2)
                    {
                        this._items[_loc_4].hasStart = true;
                        param1 = true;
                    }
                }
                _loc_4++;
            }
            if (this.lastserverTime + 60 > _loc_2 && !param1)
            {
                return;
            }
            this.lastserverTime = _loc_2;
            var _loc_6:* = new Array();
            var _loc_7:* = new Array();
            var _loc_8:* = UserData.getUserData("level");
            var _loc_9:int = 0;
            this._items[0].hasStart = false;
            this._items[1].hasStart = false;
            this._items[0].data = null;
            this._items[1].data = null;
            var _loc_10:* = this.cfgActive[1];
            var _loc_11:* = this.cfgActive[1].time;
            var _loc_12:* = this.cfgActive[1].time.split("-");
            var _loc_13:* = (this.cfgActive[1].time.split("-")[0] as String).split("#");
            var _loc_14:* = (_loc_12[1] as String).split("#");
            var _loc_15:* = _loc_3;
            var _loc_16:* = new Date(_loc_15.fullYear, _loc_15.month, _loc_15.date, int(_loc_13[0]), int(_loc_13[1]), 0, 0);
            var _loc_17:* = new Date(_loc_15.fullYear, _loc_15.month, _loc_15.date, int(_loc_14[0]), int(_loc_14[1]), 0, 0);
            var _loc_18:* = new Object();
            if (_loc_2 >= _loc_16.time / 1000 && _loc_2 <= _loc_17.time / 1000)
            {
                _loc_18.flag = true;
                _loc_18.time = _loc_17.time / 1000 - _loc_2;
                ModelProxy.change("mainui", "timePao", _loc_18);
            }
            else
            {
                _loc_18.flag = false;
                ModelProxy.change("mainui", "timePao", _loc_18);
            }
            if (_loc_8 == 0)
            {
                this._items[0].visible = false;
                this._items[1].visible = false;
                this.showmc = 0;
                return;
            }
            var _loc_19:* = "" + _loc_3.day;
            var _loc_20:* = ModelProxy.getValue("data", "hefu");
            var _loc_21:* = ModelProxy.getValue("data", "hefuday");
            for each (_loc_22 in this.allActives)
            {
                
                _loc_24 = _loc_22.week;
                _loc_25 = true;
                _loc_26 = false;
                if (_loc_22.hf)
                {
                    if (_loc_20 && _loc_21 <= 7)
                    {
                        _loc_26 = true;
                        if (!_loc_22.hf[_loc_21])
                        {
                            _loc_25 = false;
                        }
                    }
                }
                if (!_loc_26)
                {
                    if (_loc_24)
                    {
                        _loc_27 = ModelProxy.getValue("data", "openday");
                        _loc_28 = _loc_24.split("&");
                        _loc_29 = (_loc_28[0] as String).split("*");
                        _loc_30 = (_loc_28[1] as String).split("#");
                        if (_loc_27 <= 4 && int(_loc_29[0]) > 0)
                        {
                            if (_loc_29.indexOf(((_loc_27 + 1)).toString()) != -1)
                            {
                                _loc_25 = true;
                            }
                            else
                            {
                                _loc_25 = false;
                            }
                        }
                        else if (_loc_30.indexOf(_loc_19) != -1)
                        {
                            _loc_25 = true;
                        }
                        else
                        {
                            _loc_25 = false;
                        }
                    }
                }
                if (!_loc_25)
                {
                    continue;
                }
                if (_loc_22.type == 1)
                {
                    _loc_31 = _loc_22.time;
                    _loc_32 = _loc_31.split("-");
                    _loc_33 = (_loc_32[0] as String).split("#");
                    _loc_34 = (_loc_32[1] as String).split("#");
                    _loc_35 = _loc_3;
                    _loc_36 = new Date(_loc_35.fullYear, _loc_35.month, _loc_35.date, int(_loc_33[0]), int(_loc_33[1]), 0, 0);
                    _loc_37 = new Date(_loc_35.fullYear, _loc_35.month, _loc_35.date, int(_loc_34[0]), int(_loc_34[1]), 0, 0);
                    if (_loc_36.time > _loc_37.time)
                    {
                        _loc_37.time = _loc_37.time + 86400000;
                    }
                    this.endtime = _loc_37.time / 1000;
                    if (_loc_2 >= _loc_36.time / 1000 && _loc_2 <= _loc_37.time / 1000)
                    {
                        if (int(_loc_22.show) <= _loc_8)
                        {
                            if (_loc_22.id == 29)
                            {
                                ModelProxy.change("mainui", "showPaoActive", true);
                            }
                            _loc_7.push(_loc_22);
                            this._items[_loc_9].data = _loc_22;
                            this._items[_loc_9].starttime = _loc_36.time / 1000;
                            this._items[_loc_9].endtime = this.endtime;
                            this._items[_loc_9].label.text = MColor.getHMTL("[" + _loc_22.name + "]", 70) + MColor.getHMTL(" 正在进行", 52);
                            if (this.oldid != _loc_22.id)
                            {
                                ModelProxy.change("mainui", "playactive", null);
                                this.oldid = _loc_22.id;
                            }
                            this._items[_loc_9].hasStart = true;
                            _loc_9++;
                            break;
                        }
                    }
                    else if (_loc_2 < _loc_36.time / 1000)
                    {
                        if (int(_loc_22.show) <= _loc_8)
                        {
                            if (_loc_36.time / 1000 - _loc_2 < 600)
                            {
                                this._items[_loc_9].label.text = MColor.getHMTL("[" + _loc_22.name + "] ", 70) + MColor.getHMTL("倒计时:(" + TimeDataFormatUtil.getLeftTime2(_loc_36.time / 1000 - _loc_2) + ")", 56);
                                this._items[_loc_9].lodtime = _loc_36.time / 1000 - _loc_2;
                                this._items[_loc_9].data = _loc_22;
                                this._items[_loc_9].starttime = _loc_36.time / 1000;
                                this._items[_loc_9].endtime = _loc_37.time / 1000;
                                _loc_6.push(_loc_22);
                                _loc_9++;
                                break;
                            }
                        }
                    }
                }
                if (_loc_9 == 2)
                {
                    ModelProxy.change("mainui", "showEffect", {ab:"everyday", state:true});
                    break;
                }
            }
            _loc_23 = ModelProxy.getValue("data", "activeid");
            if (_loc_9 == 0)
            {
                if (_loc_23 > 0)
                {
                    ModelProxy.change("data", "activeid", 0);
                    ModelProxy.change("zone", "changemodel", null);
                }
                this._items[0].visible = false;
                this._items[1].visible = false;
                ModelProxy.change("mainui", "showactiveview", false);
                ModelProxy.change("mainui", "showEffect", {ab:"everyday", state:false});
            }
            else if (_loc_9 == 1)
            {
                if (!this._items[0].hasStart)
                {
                    if (_loc_23 > 0)
                    {
                        ModelProxy.change("data", "activeid", 0);
                        ModelProxy.change("zone", "changemodel", null);
                    }
                }
                this._items[0].visible = true;
                this._items[1].visible = false;
                ModelProxy.change("mainui", "showactiveview", true);
                ModelProxy.change("mainui", "showEffect", {ab:"everyday", state:true});
            }
            else if (_loc_9 == 2)
            {
                this._items[0].visible = true;
                this._items[1].visible = true;
                ModelProxy.change("mainui", "showactiveview", true);
                ModelProxy.change("mainui", "showEffect", {ab:"everyday", state:true});
            }
            this.showmc = _loc_9;
            return;
        }// end function

        public function set showmc(param1:int) : void
        {
            this._showmc = param1;
            if (this._showmc > 0)
            {
                if (!this.eff)
                {
                    if (!this._effLoading)
                    {
                        App.mloader3.loadSWF(Config.getUrl("assets/images/speff/horn.swf"), 1, new Handler(this.onComp));
                        this._effLoading = true;
                    }
                }
                else
                {
                    if (this.eff.parent == null)
                    {
                        this.addChild(this.eff);
                    }
                    this.eff.height = this._showmc * 25;
                    if (this._items[0] && this._items[0].hasStart)
                    {
                        this.eff.visible = true;
                    }
                    else
                    {
                        this.eff.visible = false;
                        ModelProxy.change("mainui", "stopactive", null);
                    }
                }
            }
            else
            {
                if (this.eff && this.eff.parent)
                {
                    this.eff.parent.removeChild(this.eff);
                }
                ModelProxy.change("mainui", "stopactive", null);
            }
            if (this._int != param1)
            {
                ModelProxy.change("tasktrack", "updatetaskheight", null);
                this._int = param1;
            }
            return;
        }// end function

        private function delayOpen(param1:Object) : void
        {
            if (Config.hexie == false)
            {
                DialogMgr.instance.open(AppDialog.ACTIVITY_POPUP, [param1]);
                ModelProxy.change("mainui", "annouce", [param1.name, 250]);
                ModelProxy.change("zone", "changemodel", null);
            }
            return;
        }// end function

    }
}
